package cpoo.video;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.media.Buffer;
import javax.media.Format;
import javax.media.format.RGBFormat;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;

import cpoo.exceptions.MotionException;
import cpoo.painting.DapplesCreator;

public class ClearCodec extends PreAccessCodec {
	private DapplesCreator dappleCreator;
	private Dimension size;
	
	public ClearCodec(File frameStorrageFile, File affineTransformsFile, File objectsFile, Dimension frameSize) throws MotionException {
		supportedIns = new Format[] { new RGBFormat()};
		
		dappleCreator = new DapplesCreator(frameStorrageFile, affineTransformsFile, objectsFile, frameSize);
		this.size = frameSize;
	}
	
	public String getName() {
		return "Clear Codec";
	}
	
	
	protected Image accessFrame(Buffer frame) {
		 
		BufferedImage outImage = null;
		
		BufferToImage stopBuffer = new BufferToImage((VideoFormat) frame.getFormat());
		Image stopImage = stopBuffer.createImage(frame);
		
		outImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
		Graphics og = outImage.getGraphics();
		og.drawImage(stopImage, 0, 0, size.width, size.height, null);
		dappleCreator.coverWithDapple(outImage, frame.getSequenceNumber());
		
		long t = (long) (frame.getTimeStamp() / 10000000f);
		System.err.println(
			"Post: frame #: "
				+ frame.getSequenceNumber()
				+ ", time: "
				+ ((float) t) / 100f
				+ ", len: "
				+ frame.getLength());
		//return null;
		return outImage;
	}
}
